<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Services_Language
{
	const EOL = "\n";
	
	public static function languageSelect($attributes = array(), $lang = null)
	{
		$json = new Services_JSON();
		$configs   = Gio_Core_Config_Xml::getConfig();
		$defaultLang = $configs->localization->languages->default;
		
		$selectedId = isset($attributes['selected']) ? $attributes['selected'] : $defaultLang;
		$disableId  = isset($attributes['disabled']) ? $attributes['disabled'] : null;
		
		$elementDisabled = (isset($attributes['disabled']) && $attributes['disabled'] === true)
								? ' disabled="disabled"' : '';
		
		$name  = isset($attributes['name']) ? $attributes['name'] : null;
		$id  = isset($attributes['id']) ? $attributes['id'] : null;
		
		$output 	= sprintf("<select name='%s' id='%s' viewHelperClass='%s' viewHelperAttributes='%s'%s>", 
							$name,
							$id,
							'Modules_Core_Services_Language::languageSelect',
							$json->encodeUnsafe($attributes),
							$elementDisabled) . self::EOL
					. '<option value="">---</option>' . self::EOL;
		
		if (isset($configs->localization->languages->list)) {
			foreach (explode(',', $configs->localization->languages->list) as $l) {
				$languages[$l] = explode('|', $configs->localization->languages->details->$l);
			}
		}
		
		foreach ($languages as $index => $language) {
			$selected = ($selectedId == null || $selectedId != $language[0]) ? '' : ' selected="selected"';
			$disable  = ($disableId == null || $disableId != $language[0]) ? '' : ' disabled';
			$output  .= sprintf('<option value="%s"%s%s>%s (%s)</option>',
								$index,
								$selected,
								$disable,
								$language[1],
								$language[2]) . self::EOL;
		}
		$output .= '</select>' . self::EOL;

		$viewHelperUrl = Gio_Core_View::getInstance()->url('core_locale_viewhelper');
		$scripts = <<<END
		<script type="text/javascript">
		$('#$id').bind('change', function() {
		var lang  = $(this).val();
		var total = $('.g_a_translatable').length;
		var moduleId = $('#module_id').val();
		if (lang != '') {
			$('.g_a_translatable').each(function(index) {
				var self    = this;
				var element = $(this).children()[0];
				$(self).addClass('g_a_ajax_loading');
				var data    = { 
					id: $(element).attr('id'), 
					name: $(element).attr('name'), 
					language: lang, 
					moduleId: moduleId,
					viewHelperClass: $(element).attr('viewHelperClass'), 
					viewHelperAttributes: $(element).attr('viewHelperAttributes') 
				};
			
				$.ajaxq('core_locale', {
					type: 'post',
					url: '$viewHelperUrl',
					data: data,
					success: function(response) {
						$(self).html(response).removeClass('g_a_ajax_loading');
					}
				});
			});
		}
	});
		</script>
END;
		return $output . $scripts;
	}
	
	
	/**
	 * Show the links that allows user to switch to other language
	 * 
	 * @param string $style    Can take one of following values:
	 * - locale: Show the locale (en_US, for example)
	 * - language: Show the language name (English)
	 * - flag: Show the flag image
	 * 
	 * @param string $separate
	 * @return string
	 */
	public static function languageSwitcher($style = 'locale', $separate = ' | ')
	{
		$view    = Gio_Core_View::getInstance();
		$request = Gio_Core_Request::getInstance();
		$baseUrl = $view->APP_WEB_URL;
		
		$label = '%s';
		switch ($style) {
			case 'flag':
				$label = '<img src="' . $view->APP_STATIC_SERVER . '/images/flags/%s.png"%s />';
				break;
			case 'locale':
			default:
				$label = '%s';
				break;
		}
		$configs   = Gio_Core_Config_Xml::getConfig();
		$languages = isset($configs->localization->languages->list) 
						? explode(',', $configs->localization->languages->list)
						: array($configs->localization->languages->default);
		$paramLang = $request->getParam('lang', $configs->localization->languages->default);
		foreach ($languages as $lang) {
			$className = ($lang == $paramLang) ? ' class="g_a_language_flag_active"' : ' class="g_a_language_flag"';
			$output[] = '<a href="' . $request->getRequestUri() . '/' . $lang . '">' . sprintf($label, $lang, $className) . '</a>';
		}
		return implode($separate, $output);
	}
}